#!/bin/sh

DEVICE_SN=$(bbl_3dpsn 2> /dev/null)

CRONTAB_FILE="/mnt/sdcard/x1plus/printers/$DEVICE_SN/crontab"

start() {
    if [ -f "$CRONTAB_FILE" ]; then
        mkdir -p /var/spool/cron/crontabs
        ln -sf "$CRONTAB_FILE" /var/spool/cron/crontabs/root
        echo "Starting crond..."
        start-stop-daemon -S -m -b -p /var/run/crond.pid --exec crond
        [ $? = 0 ] && echo "OK" || echo "FAIL"
    else
        echo "No crontab found, not starting crond."
    fi
}

stop() {
    printf "Stopping crond service: "
    start-stop-daemon -K -q -p /var/run/crond.pid
    [ $? = 0 ] && echo "OK" || echo "FAIL"
}

restart() {
    stop
    sleep 5
    start
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart|reload)
    restart
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
esac

exit $?
